<template>
  <div class="search">
    <Card>
      <Row @keydown.enter.native="handleSearch">
        <Form
          ref="searchForm"
          :model="searchForm"
          inline
          :label-width="70"
          class="search-form"
          @submit.native.prevent
        >
          <Form-item label="申诉理由" prop="appealReason">
            <Input
              type="text"
              v-model="searchForm.appealReason"
              placeholder="请输入申诉理由"
              clearable
              style="width: 200px"
            />
          </Form-item>
          <Form-item label="申诉人" prop="appealUsername">
            <Input
              type="text"
              v-model="searchForm.appealUsername"
              placeholder="请输入申诉人"
              :maxlength="10"
              clearable
              style="width: 200px"
            />
          </Form-item>
          <Form-item label="状态" prop="status">
            <Select
              v-model="searchForm.status"
              placeholder="请选择状态"
              clearable
              style="width: 200px"
            >
              <Option
                v-for="(item, index) in statusList"
                :key="index"
                :value="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </Form-item>
          <Button
            @click="handleSearch"
            class="search-btn"
            type="primary"
            icon="ios-search"
            >搜索</Button
          >
        </Form>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        sortable="custom"
        class="mt_10"
        :data="data"
        ref="table"
      ></Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.page"
          :total="total"
          :page-size="searchForm.rows"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
    <Modal
      v-model="detailShow"
      :title="detailAction"
      :footer-hide="detailAction == '查看'"
    >
      <div v-if="detailRow" class="modal-info">
        <div class="modal-row">
          <div>状态：</div>
          <div>
            {{
              detailRow.status == 1 ? "申诉中" : detailRow.status == 2 ? "通过" : "不通过"
            }}
          </div>
        </div>
        <div class="modal-row">
          <div>申诉人：</div>
          <div>{{ detailRow.appealUsername }}</div>
        </div>
        <div class="modal-row">
          <div>直播房间号：</div>
          <div>{{ detailRow.roomId }}</div>
        </div>
        <div class="modal-row">
          <div>申诉时间：</div>
          <div>{{ detailRow.createTime }}</div>
        </div>
        <div class="modal-row">
          <div>申诉理由：</div>
          <div>{{ detailRow.appealReason }}</div>
        </div>
        <div v-if="detailAction == '查看'" class="modal-row">
          <div>处理时间：</div>
          <div>{{ detailRow.solveTime }}</div>
        </div>
      </div>
      <div slot="footer">
        <Button type="success" @click="submitForm(true)">通过</Button>
        <Button type="primary" @click="submitForm(false)">拒绝</Button>
      </div>
    </Modal>
    <Modal v-model="refuseShow" title="审核拒绝">
      <div>
        <Input
          type="textarea"
          v-model="remarks"
          :rows="6"
          :maxlength="100"
          placeholder="请输入拒绝原因"
          clearable
        />
      </div>
      <div slot="footer">
        <Button type="text" @click="refuseShow = false">关闭</Button>
        <Button type="primary" @click="approved(3)">提交</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import * as API_Studio from "@/api/studio.js";

export default {
  name: "studio-list",
  data() {
    return {
      loading: false, // 加载状态
      searchForm: {
        // 请求参数
        page: 1,
        rows: 10,
        appealReason: null, // 申诉理由
        appealUsername: null, // 申诉人
        status: null, // 状态
      },
      statusList: [
        // 状态列表
        { value: 1, label: "申诉中" },
        { value: 2, label: "通过" },
        { value: 3, label: "不通过" },
      ],
      selectList: [], // 多选数据
      detailRow: {}, //操作当前行
      detailShow: false, // 审核、详情弹窗
      detailAction: "", // 弹窗标题
      refuseShow: false, // 审核拒绝弹窗
      remarks: "", // 拒绝原因
      columns: [
        // 表头
        { title: "序号", width: 100, type: "index" },
        { title: "申诉理由", key: "appealReason", minWidth: 180, tooltip: true },
        { title: "申诉人", key: "appealUsername", minWidth: 120, tooltip: true },
        { title: "直播房间号", key: "roomId", minWidth: 120, tooltip: true },
        { title: "申诉时间", key: "createTime", minWidth: 180, tooltip: true },
        { title: "处理时间", key: "solveTime", minWidth: 180, tooltip: true },
        {
          title: "状态",
          minWidth: 100,
          render: (h, params) => {
            let label = "申诉中";
            if (params.row.status == 2) label = "通过";
            if (params.row.status == 3) label = "不通过";
            return h("span", {}, label);
          },
        },
        {
          title: "操作",
          align: "center",
          width: 200,
          fixed: "right",
          render: (h, params) => {
            let func = [];
            if (params.row.status == "1") {
              func.push(this.btnDom(h, "warning", "解禁", "handleView", params, true));
            } else {
              func.push(this.btnDom(h, "warning", "查看", "handleView", params, true));
            }
            return h("div", {}, func);
          },
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
    };
  },
  methods: {
    // 分页 改变页码
    changePage(v) {
      this.searchForm.page = v;
      this.getData();
    },
    // 分页 改变页数
    changePageSize(v) {
      this.searchForm.rows = v;
      this.searchForm.page = 1;
      this.getData();
    },
    // 搜索
    handleSearch() {
      this.searchForm.page = 1;
      this.searchForm.rows = 10;
      this.getData();
    },
    // 按钮
    btnDom(h, type = null, label = "", method, params, ghost = false) {
      return h(
        "Button",
        {
          props: { type, size: "small", ghost },
          style: { marginRight: "5px" },
          on: {
            click: () => {
              this[method](params.row, label);
            },
          },
        },
        label
      );
    },
    // 详情
    handleView(v, label) {
      this.detailRow = v;
      this.detailAction = label;
      this.detailShow = true;
    },
    // 提交审核
    submitForm(v) {
      if (v) {
        // 通过
        this.approved(2);
      } else {
        // 拒绝
        this.detailShow = false;
        this.remarks = "";
        this.refuseShow = true;
      }
    },
    // 审核
    approved(status) {
      let params = {
        id: this.detailRow.id,
        refuseReason: status == 2 ? "" : this.remarks,
        status,
      };
      API_Studio.appealReport(params).then((res) => {
        if (res.success) {
          this.$Message.success("已" + status == 2 ? "通过" : "拒绝");
          this.detailShow = {};
          this.detailShow = false;
          this.refuseShow = false;
          this.getData();
        }
      });
    },
    //查询申诉列表
    getData() {
      this.loading = true;
      API_Studio.appealList(this.searchForm)
        .then((res) => {
          if (res.success) {
            this.data = res.result.records;
            this.total = parseInt(res.result.total);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
  mounted() {
    this.getData();
  },
};
</script>
<style lang="scss" scoped>
::v-deep .ivu-table-wrapper {
  width: 100%;
}
::v-deep .ivu-card {
  width: 100%;
}

.modal-info {
  .modal-row {
    display: flex;
    line-height: 35px;
    padding: 0 20px;
    > div:first-child {
      width: 90px;
      text-align: right;
      padding-right: 5px;
      color: #777777;
    }
    > div:last-child {
      flex: 1;
      word-break: break-all;
    }
    .header-img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background-color: #c0c0c0;
    }
  }
}
</style>
